tocbot.init({
    // Where to render the table of contents.
    tocSelector: '#tools-bar-topic',
    // Where to grab the headings to build the table of contents.
    contentSelector: '#tools-bar-content',
    // Which headings to grab inside of the contentSelector element.
    headingSelector: 'h1, h2, h3',
    // For headings inside relative or absolute positioned containers within content.
    hasInnerContainers: true,
});

$(function () {
    $('#comment-show').load('/detail/comment', {
        blogId: $('#blogId').val()
    });
})

function reply(instance) {
    const ins = $(instance);
    const parentId = ins.data('parent-id');
    const parentNick = ins.data('parent-nick');
    $('#parentCommentId').val(parentId);
    $('#content').attr('placeholder', '@' + parentNick).focus();
    $(window).scrollTo($('#comment'), 500);
}

$('#comment-submit').click(function () {
    const commentId = $('#parentCommentId');
    const nick = $('#nick');
    const email = $('#email');
    const content = $('#content');

    const data = {
        blogId: $('#blogId').val(),
        parentCommentId: commentId.val(),
        nick: nick.val(),
        email: email.val(),
        content: content.val()
    };

    let needBreak = false;
    $.each(data, function (key, value) {
        if (typeof (value) === 'string' && value.length === 0) {
            if ('parentCommentId' === key) return true;
            alert(key + ' can not be empty !');
            needBreak = true;
            return false;
        }
    });
    if (needBreak) return;

    $('#comment-show').load('/detail/comment/submit', data, function (result) {
        cocoMessage.info(result.msg, function () {
            commentId.val('');
            nick.val('');
            email.val('');
            content.val('');
            $('#content').attr('placeholder', '内容*' );
        })
    });
});

$('#toTop').click(function() {
    $(window).scrollTo(0, 500);
});